package ee.greaton.kultuuriaken;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class EventDBAdapter {
	public static final String KEY_TITLE = "title";
	public static final String KEY_DESC = "description";
	public static final String KEY_DATE = "date";
	public static final String KEY_TIME = "time";
	public static final String KEY_ETIME = "etime";
	public static final String KEY_IMG = "imglink";
	public static final String KEY_LOC = "location";
	public static final String KEY_CAT = "category";
	public static final String KEY_PRICE = "ticketprice";
	public static final String KEY_LAT = "geolat";
	public static final String KEY_LONG = "geolong";
	public static final String KEY_URL = "ticketurl";
	private static final String DATABASE_TABLE = "events";
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (title text, description text, date text, "
			+ "time text, imglink text, location text, "
			+ "category text, ticketprice text, geolat text, geolong text, etime text, ticketurl text, UNIQUE(title, date, time) ON CONFLICT REPLACE);";
	private DatabaseHelper DBHelper;
	SQLiteDatabase db;

	public EventDBAdapter(Context context) {
		DBHelper = new DatabaseHelper(context, DATABASE_TABLE,
				DATABASE_VERSION, DATABASE_CREATE);
	}

	public EventDBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		DBHelper.close();
	}

	public void clearEvents() {
		db.delete(DATABASE_TABLE, null, null);
	}

	public void addEvent(Event event) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TITLE, event.getTitle());
		initialValues.put(KEY_DESC, event.getDescription());
		initialValues.put(KEY_DATE, event.getDate());
		initialValues.put(KEY_TIME, event.getTime());
		initialValues.put(KEY_IMG, event.getImgLink());
		initialValues.put(KEY_LOC, event.getLocation());
		initialValues.put(KEY_CAT, event.getCategory());
		initialValues.put(KEY_PRICE, event.getTicketPrice());
		initialValues.put(KEY_LAT, event.getGeoLat());
		initialValues.put(KEY_LONG, event.getGeoLong());
		initialValues.put(KEY_ETIME, event.getEtime());
		initialValues.put(KEY_URL, event.getTicketUrl());
		db.insert(DATABASE_TABLE, null, initialValues);
	}

	public int getCount(String search) {
		Cursor cursor = getAll(search);
		return cursor.getCount();
	}

	public Cursor getAll(String search) {
		if(search != null){
			return db.query(DATABASE_TABLE, null, KEY_TITLE + " LIKE '%" + search + "%' ", null, null, null, KEY_DATE + " ASC, CASE " + KEY_TIME + " WHEN '' THEN 1 ELSE 0 END, " + KEY_TIME +  " ASC");
		}else{
			return db.query(DATABASE_TABLE, null, null, null, null, null, KEY_DATE + " ASC, CASE " + KEY_TIME + " WHEN '' THEN 1 ELSE 0 END, " + KEY_TIME +  " ASC");
		}
	}
}
